<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 文章分类管理
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: category.php 1 2014-01-13 15:05:51Z zhouyl $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Controller_Admin_Article_Category extends Controller_Admin_Base
{

    /**
     * 分类管理首页
     */
    public function action_index()
    {
        $this->privilege('article.category.list');
    }

    /**
     * 输出 dtree 树
     */
    public function action_dtree()
    {
        $this->view->categories = Model::factory('article_category')->all();
    }

    /**
     * 获取 JS 脚本
     */
    public function action_js()
    {
        $this->view->categories = Model::factory('article_category')->all();
    }

    /**
     * 添加分类信息
     */
    public function action_add()
    {
        $this->privilege('article.category.add');

        $model = Model::factory('article_category');

        if ($this->request->is_post())
        {
            if ( ! $model->add($this->request->post()))
            {
                $errors = $model->errors('admin.article.category');
                echo current($errors);
            }
            exit;
        }
    }

    /**
     * 编辑分类信息
     */
    public function action_edit()
    {
        $this->privilege('article.category.edit');

        $cat_id = $this->request->param('id');
        $model = Model::factory('article_category');
        $category = $model->get($cat_id);

        if ( ! $category)
        {
            $this->message('admin.article.category.not_found')
                ->render();
        }

        if ($this->request->is_post())
        {
            if ( ! $model->edit($cat_id, $this->request->post()))
            {
                $errors = $model->errors('admin.article.category');
                echo current($errors);
            }
            exit;
        }

        $this->view->set($category);
    }

    /**
     * 删除分类信息
     */
    public function action_del()
    {
        $this->privilege('article.category.del');

        $cat_id = $this->request->param('id');
        $model = Model::factory('article_category');

        if ( ! $model->del($cat_id))
        {
            $errors = $model->errors('admin.article.category');
            echo current($errors);
        }
        exit;
    }

}